package com.lm.scau.dyb.db.Dalx;

import android.content.ContentValues;
import android.database.Cursor;

import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.bean.MovieRole;
import com.lm.scau.dyb.bean.PhotoWall;
import com.lm.scau.dyb.db.DybDB;
import com.lm.scau.dyb.utils.FlagHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by Administrator on 2016/8/20.
 */
public class RoleDalx extends BaseDalx {

    private static final String TB_NAME = "tb_role";
    private static final String RO_ID = "ro_id";
    private static final String RO_NAME = "ro_name";
    private static final String RO_ALPHA = "ro_alpha";
    private static final String RO_LETTER = "ro_letter";
    private static final String RO_URL = "ro_url";
    private static final String RO_MOVIE_ID = "movie_id";

    public RoleDalx() {
    }

    public RoleDalx(BaseActivity activity) {
        if (db == null) {
            db = DybDB.get(activity);
        }
        if (!db.tabbleIsExist(TB_NAME)) {
            createDataFileTable();
        }
    }

    public static void main(String args[]) {
        new RoleDalx().createDataFileTable();
    }

    private void createDataFileTable() {
        String createTableSql = "CREATE TABLE IF NOT EXISTS " + TB_NAME + "(" + RO_ID
                + " int, " + RO_NAME + " varchar, " + RO_ALPHA + " varchar, " + RO_LETTER + " varchar, " +
                RO_URL + " varchar, " + RO_MOVIE_ID + " varchar)";
        db.creatTable(createTableSql);
    }

    /**
     * 获取最新的轮播时间，用来更新最新数据
     *
     * @return
     */
    @Override
    public int getId() {
        String sql = "select " + RO_ID + " from " + TB_NAME +
                " order by " + RO_ID + " desc limit ?";
        Cursor cursor = db.find(sql, new String[]{"1"});
        int id = -1;

        while (cursor.moveToNext()) {
            id = cursor.getInt(cursor.getColumnIndex(RO_ID));
            break;
        }
        return id;
    }

    /**
     * 新增轮播数据
     *
     * @param role
     * @return
     */
    public boolean addMovieRoles(MovieRole role) {
        boolean isOk = false;
        ContentValues values = new ContentValues();
        values.put(RO_ID, role.getRo_id());
        values.put(RO_NAME, role.getRo_name());
        values.put(RO_ALPHA, role.getRo_alpha());
        values.put(RO_LETTER, role.getRo_letter());
        values.put(RO_MOVIE_ID, role.getMovie_id());
        values.put(RO_URL, role.getRo_url());
        if (db.save(TB_NAME, values)) {
            isOk = true;
        }
        return isOk;
    }

    /**
     * 根据类型获取到所有影片的演员列表，方便检索
     *
     * @param type
     * @return
     */
    public List<MovieRole> getRoleBytype(String type) {
        List<MovieRole> roles = new ArrayList<>();
        Map<String, MovieRole> maps = new LinkedHashMap<>();
        String sql = "select " + TB_NAME + ".* from tb_movie" +
                " inner join " + TB_NAME +
                " on tb_movie.mv_type like ? " +
                " and tb_movie.mv_id = " + RO_MOVIE_ID +
                " order by " + RO_LETTER;
        Cursor cursor = db.find(sql, new String[]{"%" + type + "%"});
        while (cursor.moveToNext()) {
            MovieRole role = new MovieRole();
            role.setRo_id(cursor.getInt(cursor.getColumnIndex(RO_ID)));
            role.setRo_name(cursor.getString(cursor.getColumnIndex(RO_NAME)));
            role.setRo_letter(cursor.getString(cursor.getColumnIndex(RO_LETTER)));
            role.setRo_alpha(cursor.getString(cursor.getColumnIndex(RO_ALPHA)));
            role.setMovie_id(cursor.getString(cursor.getColumnIndex(RO_MOVIE_ID)));
            role.setRo_url(cursor.getString(cursor.getColumnIndex(RO_URL)));
            maps.put(role.getRo_name(), role); //可以去重，名字相同的只取一个即可
        }
        if (maps.size() > 0) {
            Set<String> keys = maps.keySet();
            for (String key : keys) {
                roles.add(maps.get(key));
            }
        }
        return roles;
    }
}
